package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.service.FwhfService;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.MmrdT;
import cn.e4j.modular.crm.entity.MmrnT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yuan_rz on 2018/04/12.
 */
@RestController
@RequestMapping("api/wx/fwhf")
public class FwhfController extends ControllerSupport {
    private static Logger logger = LoggerFactory.getLogger(FwhfController.class);

    @Autowired
    private FwhfService fwhfService;

    @Autowired
    Gwat200Service gwat200Service;

    /**
     * 获取服务单列表
     *
     * @return
     */
    @RequestMapping(value = "getServiceOrderListData")
    public ResponseData getServiceOrderListData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        SysUser currentUser = wapper.getSysUser();
        try {

            //            / 获取区域权限-进行省市过滤数据
            inDto.put("GWAA003", currentUser.getACCOUNT());
            inDto.put("MMRE004S",gwat200Service.getMmreData(inDto));

            inDto.put("MMRDDOCDATE", DateUtils.parseDate(inDto.getString("MMRDDOCDATE")));
            list = fwhfService.getServiceOrderListData(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 服务回访提交
     *
     * @return
     */
    @RequestMapping(value = "saveFwhf")
    public ResponseData saveFwhf(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        Date date = new Date();
        List<MmrnT> mmrn_tPos = new ArrayList<>();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
            String mmrndocno = "WXHF" + DateUtils.getDate("yyyyMMddHHmmssSSS");

            JSONArray pjs = inJson.getJSONArray("pj");
            for (int i = 0; i < pjs.size(); i++) {
                JSONObject dto = pjs.getJSONObject(i);
                MmrnT mmrn_tPO = new MmrnT();
                mmrn_tPO.setMMRNENT(user.getDBC());
                mmrn_tPO.setMMRNDOCNO(mmrndocno);
                mmrn_tPO.setMMRNSEQ(BigDecimal.valueOf(i + 1));
                //mmrn_tPO.setMMRNDOCDATE(DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd)));
                mmrn_tPO.setMMRNDOCDATE(LocalDate.now());
                mmrn_tPO.setMMRN001(inDto.getString("MMRDDOCNO"));
                mmrn_tPO.setMMRN002(inDto.getString("MMRDSEQ"));
                mmrn_tPO.setMMRN003(inDto.getString("MMRJ001"));
                mmrn_tPO.setMMRN004(dto.getString("MMRK004"));
                mmrn_tPO.setMMRN005(inJson.getString("MMRN005"));
                //mmrn_tPO.setMmrn006(String.valueOf(dto.getString("mmrd015")));
                mmrn_tPO.setMMRN007(inJson.getString("MMRN007"));
                mmrn_tPO.setMMRN008(inJson.getString("MMRN008"));
                mmrn_tPO.setMMRN009(inJson.getString("MMRN009"));
                mmrn_tPO.setMMRN009(inJson.getString("MMRN010"));
                mmrn_tPO.setMMRN011(user.getID());
                mmrn_tPO.setMMRNACTI("Y");
                mmrn_tPO.setMMRNUSER(user.getID());
                mmrn_tPO.setMMRNGRUP(user.getORG_ID());
                mmrn_tPO.setMMRNORIU(user.getID());
                mmrn_tPO.setMMRNORIG(user.getORG_ID());
                //mmrn_tPO.setMMRNDATE(date);
                mmrn_tPO.setMMRNDATE(LocalDate.now());
                mmrn_tPO.setMMRNTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
                mmrn_tPos.add(mmrn_tPO);
            }

            MmrdT mmrd_tPO = new MmrdT();
            mmrd_tPO.setMMRD020("6");
            mmrd_tPO.setMMRDENT(user.getDBC());
            mmrd_tPO.setMMRDDOCNO(inDto.getString("MMRDDOCNO"));
            mmrd_tPO.setMMRDSEQ(inDto.getBigDecimal("MMRDSEQ"));
            mmrd_tPO.setMMRDMODU(user.getID());

            fwhfService.saveFwhf(mmrn_tPos, mmrd_tPO);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

}
